package com.java.example.app

import android.content.Intent
import cn.jpush.android.api.JPushInterface
import com.blankj.utilcode.util.ActivityUtils
import com.blankj.utilcode.util.CacheDiskUtils
import com.blankj.utilcode.util.SPUtils
import com.java.example.bean.UserProfile
import com.java.example.ui.activity.LoginActivity
import com.java.example.ui.activity.MainActivity
import com.java.example.util.AccountUtil.clearAccount


/**
 * Created by chenll on 2024/8/22 0022.
 */
object AccountManager {
    private var mUserProfile: UserProfile? = null

    fun setUserProfile(userProfile: UserProfile?) {
        mUserProfile = userProfile
    }

    @JvmStatic
    val currentUser: UserProfile?
        get() {
            if (mUserProfile != null) {
                return mUserProfile
            }
            return null
        }

    private fun clearUserProfile() {
        if (mUserProfile != null) {
            mUserProfile = null
        }
    }

    fun logout() {
        //应该先判断当前的delegate，如果已经在login了，就不做处理
        val activity = Configurator.latteConfigs[ConfigKeys.ACTIVITY] as MainActivity?
        // 由于退出时，是重新开启一个新的activity，所以一定要把蓝牙断开
        BleDeviceManager.getInstance().disconnect()
        SPUtils.getInstance().put(Constant.IS_LOGIN_STATE, false)
        clearAccount()
        if (!JPushInterface.isPushStopped(Latte.applicationContext)) {
            JPushInterface.stopPush(Latte.applicationContext)
        }
        clearAccount(activity!!, Constant.USER_CACHE)
        ActivityUtils.finishAllActivities()
        activity.startActivity(Intent(activity, LoginActivity::class.java))
    }

    private const val lastTime: Long = 0

    fun clearAccount() {
        //将账户清空
        clearUserProfile()
        SPUtils.getInstance().remove(Constant.USER_NAME)
        SPUtils.getInstance().remove(Constant.USER_PASSWORD)
        CacheDiskUtils.getInstance().clear()
        clearAccount(Latte.applicationContext, Constant.USER_CACHE)
        CacheDiskUtils.getInstance().remove(Constant.USER_CACHE)

    }
}
